<template>
    <div class="login-wrap">
        <div class="ms-login">
          <div class="ms-title">博客小栈后台管理</div>
            <el-tabs v-model="activeName" :stretch="true" @click="handleTabClick">
                <el-tab-pane label="账号登录" name="first" >
                    <el-form :model="loginForm1" :rules="rules" ref="loginForm1" label-width="0px" class="ms-content">
                        <el-form-item prop="username">
                            <el-input 
                                prefix-icon="el-icon-s-custom" 
                                v-model="loginForm1.username" 
                                placeholder="用户名">
                            </el-input>
                        </el-form-item>
                        <el-form-item prop="password">
                            <el-input 
                                prefix-icon="el-icon-lock" 
                                :type="pwdType" 
                                v-model="loginForm1.password" 
                                auto-complete="on" 
                                placeholder="密码" 
                                @keyup.enter.native="handleLogin">
                            <i slot="suffix" class="el-icon-view" @click="showPwd"></i>
                            </el-input>
                        </el-form-item>
                    </el-form>
                </el-tab-pane>
                <el-tab-pane label="免密登录" name="second">
                    <el-form :model="loginForm2" :rules="rules2" ref="loginForm2" label-width="0px" class="ms-content">
                        <el-form-item prop="phone">
                            <el-input 
                                prefix-icon="el-icon-mobile-phone" 
                                v-model="loginForm2.phone" 
                                placeholder="手机号"
                                clearable>
                            </el-input>
                        </el-form-item>
                    </el-form>
                    <el-form :model="loginForm3" :rules="rules3" ref="loginForm3" label-width="0px" class="ms-content">
                        <el-form-item prop="code">
                            <el-input  v-model="loginForm3.code"  auto-complete="on"  placeholder="6位数验证码" @keyup.enter.native="handleLogin">
                                <el-button :disabled="msgDisabled" type="text" slot="append" @click="handleGetCode()" style="width:100px;color:#57a3f3">{{codeMsg}}</el-button>
                            </el-input>
                        </el-form-item>
                    </el-form>
                </el-tab-pane>
                <div class="login-btn">
                    <el-button type="primary" @click="handleLogin('loginForm')" :loading="loading">登录</el-button>
                </div>
            </el-tabs>
        </div>
        <div v-if="$store.state.settings.showFooter" id="el-login-footer">
            <span v-html="$store.state.settings.footerTxt" />
            <span> ⋅ </span>
            <a href="http://www.beian.miit.gov.cn" target="_blank">{{ $store.state.settings.caseNumber }}</a>
        </div>

    </div>
</template>

<script>
import { checkPhone } from '@/utils/validate'
import { sendMsg } from '@/api/user'
export default {
    data: function () {
        const validatePass = (rule, value, callback) => {
            if (value.length < 5) {
                callback(new Error('密码不能小于5位'))
            } else {
                callback()
            }
        }
        return {
            waitTime:60,
            codeMsg:'获取验证码',
            activeName:'first',
            msgDisabled:false,

            loginForm1: {
                username: 'admin',
                password: '123456',
            },
            loginForm2: {
                phone: '15685482010',
            },
            loginForm3: {
                code: '',
            },
            rules: {
                username: [{ required: true, message:'用户名不能为空' }],
                password: [{ required: true, validator: validatePass }]
            },
            rules2:{
                phone: [{ required: true, message:'请输入手机号码' },{trigger:'blur',validator:checkPhone}]
            },
            rules3:{
                code:  [{ required: true, message:'请输入验证码' }]
            },
            loading: false,
            pwdType: 'password',
        }
    },
    methods: {
        showPwd() {
            if (this.pwdType === 'password') {
                this.pwdType = ''
            } else {
                this.pwdType = 'password'
            }
        },
        handleLogin() {
            let valid = false
            if(this.activeName ==='first'){
                this.$refs.loginForm1.validate(valid1 => {
                    valid1 && (valid = true)
                })
            }else if(this.activeName ==='second'){
                this.$refs.loginForm2.validate(valid2 => {
                    if(valid2){
                         this.$refs.loginForm3.validate(valid3 => {
                            valid3 && (valid = true)
                        })
                    }
                })
            }
            if(valid){
                this.loading = true
                let params = {}
                if(this.activeName === 'first'){
                    params.username = this.loginForm1.username
                    params.password = this.loginForm1.password
                }else{
                    params.phone    = this.loginForm2.phone
                    params.code     = this.loginForm3.code
                }
                this.$store.dispatch('user/Login', params).then((res) => {
                    if(res.code === 200){
                        this.loading = false
                        this.$router.push({ path: this.redirect || '/' })
                    }
                }).catch(() => {
                    this.loading = false
                })
            }else {
                console.log('error submit!!')
                return false
            }
        },
        handleTabClick(){
            if(this.activeName === 'first'){
                this.loginForm2 = {}
                this.loginForm3 = {}
            }else{
                this.loginForm1 = {}
            }
        },
        handleGetCode(){
            this.$refs.loginForm2.validate(valid => {
                if(valid){
                    this.startCountdown()
                    this.getMsg()
                }
            })
        },
        //开始倒计时
        startCountdown(){
            this.msgDisabled = true
            let interval = setInterval(()=> {
                this.codeMsg = this.waitTime+'后重新发送';
                this.waitTime --
                if(this.waitTime < 0) {
                    this.codeMsg = '重新发送'
                    this.waitTime = 10
                    this.msgDisabled = false
                    clearInterval(interval);
                }
            }, 1000);
        },
        getMsg(){
            this.apiSendMsg(this.loginForm2.phone)
        },
        //==========>请求api接口<==========//
        apiSendMsg(phone){
            sendMsg(phone).then(res =>{
                if(res.code){
                    console.log(res.message)
                }
            })
        //==========>请求api接口<==========//
        }
    }
}
</script>

<style scoped>
  .login-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-image: url(../../assets/imgs/back.jpg);
    background-size: 100%;
  }

  .ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: #fff;
    border-bottom: 1px solid #ddd;
  }

  .ms-login {
    position: absolute;
    left: 50%;
    top: 45%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);
    overflow: hidden;
  }

  .ms-content {
    margin: 20px 0;
    padding: 0px 30px;
  }
  .login-btn {
    text-align: center;
  }

  .login-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
  }

  .login-tips {
    font-size: 12px;
    line-height: 30px;
    color: #fff;
  }
  .password{
      display: flex;
  }
</style>
